package com.shoes.service.impl;

import com.shoes.dao.StoreDAO;
import com.shoes.dao.UserDAO;
import com.shoes.model.CouponDO;
import com.shoes.model.ResponseDO;
import com.shoes.model.StoreDO;
import com.shoes.model.UserCouponDO;
import com.shoes.service.StoreService;
import com.shoes.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author asen
 */
@Service("storeService")
public class StoreServiceImpl implements StoreService {

    private StoreDAO storeDAO;
    private UserDAO userDAO;

    @Override
    public ResponseDO getCouponsByStoreId(long storeId) throws Exception {
        ResponseDO result;
        List<CouponDO> list = storeDAO.queryCouponListByStoreId(storeId);
        if(list == null) {
            result = new ResponseDO(ResultCode.COUPON_EMPTY, null);
        } else {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL, list);
        }
        return result;
    }

    @Override
    public ResponseDO getStoreInfo(long userId, long storeId) throws Exception {
        return null;
    }

    @Override
    public ResponseDO createStore(long userId, String headPortrait, String storeName) throws Exception {
        ResponseDO result;
        StoreDO store = new StoreDO();
        store.setStoreId(userId);
        store.setHeadPortrait(headPortrait);
        store.setStoreName(storeName);

        int count = storeDAO.insertStore(store);
        if(count == 1) {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL, store.getStoreId(), null);
        } else {
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null, null);
        }
        return result;
    }

    @Override
    public ResponseDO setupStoreInfo(long storeId, String headPortrait, String storeName, String wxNum, String storeBackground, String storeAddress) throws Exception {
        ResponseDO result;
        StoreDO store = new StoreDO();
        store.setStoreId(storeId);
        store.setHeadPortrait(headPortrait);
        store.setStoreName(storeName);
        store.setStoreBackground(storeBackground);
        store.setStoreAddress(storeAddress);

        int count = storeDAO.updateStoreInfo(store);
        if(count == 1) {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL, null, null);
        } else {
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null, null);
        }
        return result;
    }

    @Override
    public ResponseDO setupDefaultFreight(long storeId, long regionId, int goodsNum, double freight, int goodsNumAdd, double freightAdd) throws Exception {
        ResponseDO result;

        BigDecimal freightBD = BigDecimal.valueOf(freight);
        BigDecimal freightAddBD = BigDecimal.valueOf(freightAdd);

        int count = storeDAO.insertDefaultFreight(storeId, regionId, goodsNum, freightBD, goodsNumAdd, freightAddBD);
        if(count == 1) {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL, null, null);
        } else {
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null, null);
        }
        return result;
    }

    @Override
    public ResponseDO setCashOnDelivery(long storeId, int cod) throws Exception {
        ResponseDO result;
        if(cod > 1) {
            cod = 1;
        }
        boolean updated = storeDAO.updateCashOnDelivery(storeId, cod);
        if(updated) {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL,null);
        } else {
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }
        return result;
    }

    @Override
    public ResponseDO setSeventhDaysReturn(long storeId, int seventh) throws Exception {
        ResponseDO result;
        if(seventh > 1) {
            seventh = 1;
        }
        boolean updated = storeDAO.updateSeventhDaysReturn(storeId, seventh);
        if(updated) {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL,null);
        } else {
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }
        return result;
    }

    @Override
    public ResponseDO setGuaranteeBusiness(long storeId, int guarantee) throws Exception {
        ResponseDO result;
        if(guarantee > 1) {
            guarantee = 1;
        }
        boolean updated = storeDAO.updateGuaranteeBusiness(storeId, guarantee);
        if(updated) {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL,null);
        } else {
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }
        return result;
    }

    @Override
    public ResponseDO createCoupon(long storeId, double couponValue, long startTime,
           long endTime, double minAmount, int couponNum, int limitNum) throws Exception {
        ResponseDO result;
        CouponDO coupon = new CouponDO();

        coupon.setCouponValue(BigDecimal.valueOf(couponValue));
        coupon.setStartTime(new Timestamp(startTime));
        coupon.setEndTime(new Timestamp(endTime));
        coupon.setMinAmount(BigDecimal.valueOf(minAmount));
        coupon.setCouponNum(couponNum);
        coupon.setLimitNum(limitNum);

        int count = storeDAO.insertCoupon(coupon);
        if(count == 1) {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL, null, null);
        } else {
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null, null);
        }
        return result;
    }

    @Override
    public ResponseDO deleteCoupon(long storeId, String id) throws Exception {
        ResponseDO result;
        int count = storeDAO.deleteCoupon(storeId, id);
        if(count == 1) {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL, null, null);
        } else {
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null, null);
        }
        return result;
    }

    @Override
    public ResponseDO receiveCoupon(long userId, long storeId, long couponId) throws Exception {
        ResponseDO result;

        Date now = new Date();
        SimpleDateFormat time = new SimpleDateFormat("yyyyMMddHHmmss");
        Random random = new Random();
        String couponSn = time.format(now) + (random.nextInt(9000) + 1000);

        int count = storeDAO.insertUserCouponRecord(userId, couponId, couponSn);
        if(count == 1) {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL, null, null);
        } else {
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null, null);
        }
        return result;
    }

    @Override
    public ResponseDO getCouponDetail(long storeId) throws Exception {
        ResponseDO result;
        List<UserCouponDO> list = storeDAO.queryUserCouponListByStoreId(storeId);
        if(list == null) {
            result = new ResponseDO(ResultCode.GLOBAL_RESULT_EMPTY, null);
        } else {
            for(UserCouponDO uc : list) {
                uc.setUserName(userDAO.queryUserNameByUserId(uc.getUserId()));
            }
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL, list);
        }
        return result;
    }

    public StoreDAO getStoreDAO() {
        return storeDAO;
    }
    @Autowired
    public void setStoreDAO(StoreDAO storeDAO) {
        this.storeDAO = storeDAO;
    }

    public UserDAO getUserDAO() {
        return userDAO;
    }
    @Autowired
    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }
}
